﻿<common:LayoutAwarePage xmlns:UserControls="using:CrmField.Win8App.UserControls" 
    x:Name="pageRoot"
    x:Class="CrmField.Win8App.ItemDetailPage"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:CrmField.Win8App"
    xmlns:common="using:CrmField.Win8App.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:callisto="using:Callisto.Controls"
    mc:Ignorable="d">

    <Page.Resources>

        <!-- Collection of items displayed by this page -->
        <CollectionViewSource
            x:Name="itemsViewSource"
            Source="{Binding Items}"/>

        <common:DateTimeToStringConverter x:Key="dateTimeToStringConverter" />
        <common:StateToEnableConverter x:Key="stateToEnableConverter" />
    </Page.Resources>

    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid Style="{StaticResource LayoutRootStyle}">
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition />
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition x:Name="itemsColumn" Width="Auto"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <!-- Vertical scrolling item list -->
        <ListView
            x:Name="itemListView"
            AutomationProperties.AutomationId="ItemsListView"
            AutomationProperties.Name="Items"
            TabIndex="1"
            Grid.Row="1"
            Grid.Column="0"
            Grid.RowSpan="2"
            Margin="-10,-10,0,0"
            Padding="120,0,0,60"
            ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
            IsSwipeEnabled="False"
            SelectionChanged="ItemListView_SelectionChanged"
            ItemTemplate="{StaticResource Standard130ItemTemplate}"/>

        <!--
            The remainder of the page is one large FlipView that displays details for
            one item at a time, allowing the user to flip through all items in the chosen
            group
        -->
        <FlipView
            x:Name="flipView"
            AutomationProperties.AutomationId="ItemsFlipView"
            AutomationProperties.Name="Item Details"
            TabIndex="1"
            Grid.RowSpan="2"
            Grid.Row="1"
            Grid.Column="1"
            ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
            >
 
            <FlipView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="120"/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <RichTextBlock x:Name="richTextBlock" 
                                       Style="{StaticResource ItemRichTextStyle}" 
                                       IsTextSelectionEnabled="False"
                                       HorizontalAlignment="Right"
                                       Margin="0,5,15,5">
                            <Paragraph>
                                <Run FontSize="22" FontWeight="Light" Text="{Binding TimeAndSubject}"/>
                                <LineBreak/>
                                <Run FontWeight="Normal" Text="{Binding Location}"/>
                            </Paragraph>
                            <Paragraph LineStackingStrategy="MaxHeight">
                                <InlineUIContainer>
                                    <StackPanel Orientation="Horizontal" Margin="5,5,5,0">
                                        <Button Content="Complete" Click="Button_Click_Complete" IsEnabled="{Binding StatusCode, Converter={StaticResource stateToEnableConverter}}" />
                                        <Button Content="Reschedule" Click="Button_Click_Reschedule" />
                                    </StackPanel>
                                </InlineUIContainer>
                            </Paragraph>
                        </RichTextBlock>
                        <ScrollViewer x:Name="scrollViewer" Style="{StaticResource HorizontalScrollViewerStyle}" Grid.Row="1">
                            <UserControls:MapsViewer Name="MapViewer" Margin="10,0,10,10" Address="{Binding Location}" />
                        </ScrollViewer>
                    </Grid>
     
                </DataTemplate>
            </FlipView.ItemTemplate>
        </FlipView>

        <!-- Back button and page title -->
        <Grid Grid.ColumnSpan="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
            <TextBlock x:Name="pageTitle" Text="{Binding Group.FormatedDate}" Style="{StaticResource PageHeaderTextStyle}" Grid.Column="1" IsHitTestVisible="false"/>
        </Grid>

        <callisto:CustomDialog x:FieldModifier="public" x:Name="rescheduleDialog" 
                               Title="Reschedule Appointment" Foreground="Black" 
                               Background="White" BackButtonVisibility="Collapsed">
            <StackPanel MinWidth="400">
                <UserControls:DateTimePicker x:Name="dateTimePicker" Margin="10" />
                <StackPanel Orientation="Horizontal">
                    <Button Content="Update" Click="Button_Click_UpdateStartcheduleDate" />
                    <Button Content="Cancel" Click="Button_Click_CancelReschedule" />
                </StackPanel>
            </StackPanel>
        </callisto:CustomDialog>

        
        
        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <!-- The back button respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!-- The back button and title have different styles when snapped -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="itemListView" Storyboard.TargetProperty="Visibility" >
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="itemsColumn" Storyboard.TargetProperty="Width" >
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>

    <Page.BottomAppBar>
        <AppBar x:Name="SecondaryTileAppBar" Padding="10,0,10,0" >
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30*"/>
                </Grid.ColumnDefinitions>
                <StackPanel x:Name="RightPanel" Orientation="Horizontal" Grid.Column="1" HorizontalAlignment="Right">
                    <Button Style="{StaticResource PinAppBarButtonStyle}" Name="PinToAppBarButton" Click="pinToAppBar_Click" />
                </StackPanel>
            </Grid>
        </AppBar>
    </Page.BottomAppBar>


</common:LayoutAwarePage>
